<?php

/**
 * SchoolManagement
 * Class to manage the schools and classes  
 */
class SchoolManagement
{
	private $classID = null;
	private $classDetails = null;

	/**
	 * formValues
	 * @var array The form values to manage teachers
	 */
	private $formValues = array
	(
		'firstname' => 'Voornaam',
		'lastname' => 'Achternaam',
		'username' => 'Gebruikersnaam',
		'password' => 'Wachtwoord',
		'password2' => 'Wachtwoord herhalen'
	);
	
	
	/**
	 * showSchoolList
	 * prints a formatted table of the schools
	 */
	function showSchoolList()
	{
		echo '<h1>Scholen beheer</h1>';
		echo '<p>Om de klassen van een school te beheren kunt u op de naam van de school klikken.</p>';
		$scholen = mysql_query("SELECT * FROM `scholen` ORDER BY naam ASC");

		echo '<a href="scholenbeheer/add_school" class="button_a">School toevoegen</a><table><tr><th>Naam school</th><th>Acties</th></tr>';

		while($school = mysql_fetch_assoc($scholen))
		{
			echo '<tr>
				<td><a href="scholenbeheer/show_school/' . $school['id'] . '">' . $school['naam'] . '</a></td>
				<td><a href="scholenbeheer/edit_school/' . $school['id'] . '">wijzig</a></td>
			</tr>';
		}

		echo '</table>';
	}

	/**
	 * showAddSchoolForm()
	 * shows the add school form, and validation text if available 
	 */
	function showAddSchoolForm()
	{
		echo '<h1>School toevoegen</h1>';

		// If form is submitted, verify and process
		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$errors = $this->verifyAddSchoolForm();

			if($errors != null)
			{
				echo '<p class="error">' . $errors . '</p>';
			}
			else
			{
				if(mysql_query("INSERT INTO `scholen` (naam) VALUES ('" . $_POST['name'] . "')"))
				{
					echo '<h1>School</h1><p>De school is succesvol aangemaakt, klik <a href="scholenbeheer/">hier</a> om terug te gaan, of vul het formulier hieronder in om nog een school aan te maken.</p>';
					$schoolMade = true;
				}
				else
				{
					echo '<h1>Foutmelding</h1><p>Er is iets mis gegaan tijdens het aanmaken van de school, probeer het alstublieft opnieuw.</p>';
				}
			}
		}

		$nameValue = (isset($_POST['name']) && !empty($_POST['name']) && !isset($schoolMade)) ? $_POST['name'] : '';
		require 'schoolform.php';
	}

	/**
	 * verifyAddSchoolForm()
	 * Verifies the submitted data
	 * 
	 * @return string Returns error data if encountered, else return null 
	 */
	private function verifyAddSchoolForm($ignoreID = null)
	{
		$errors = array();

		// all required forms filled in?
		if(empty($_POST['name']))
		{
			$errors['name'] = 'U moet een naam van een school invullen.';
		}

		// username taken?
		if(!isset($errors['name']))
		{
			if(isset($ignoreID))
			{
				$usernameTaken = mysql_num_rows(mysql_query("SELECT `id` FROM `scholen` WHERE `naam`='" . $_POST['name'] . "' AND `id` != '" . $ignoreID . "'"));
			}
			else
			{
				$usernameTaken = mysql_num_rows(mysql_query("SELECT `id` FROM `scholen` WHERE `naam`='" . $_POST['name'] . "'"));
			}

			if($usernameTaken != 0)
			{
				$errors['username'] = 'Er bestaat al een school met de naam "<strong>' . $_POST['name'] . '</strong>", kies een andere naam.';
			}
		}

		// errors? return them
		if(count($errors) > 0)
		{
			return "<ul><li>" . implode("</li><li>", $errors) . "</li></ul>";
		}
	}

	/**
	 * showEditSchoolForm()
	 * shows the edit school form, and processes the submitted data
	 * 
	 * @param int $schoolID
	 * @return string 
	 */
	function showEditSchoolForm($schoolID)
	{
		$school = mysql_fetch_assoc(mysql_query("SELECT * FROM `scholen` WHERE `id`='" . $schoolID . "'"));

		if(gettype($school) != 'array')
		{
			return "School niet gevonden.";
		}

		echo '<h1>School "' . $school['naam'] . '" bewerken</h1><p>Wanneer u het wachtwoord niet wilt veranderen kunt u deze velden leeg laten.</p>';

		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$errors = $this->verifyAddSchoolForm($_GET['school']);

			if($errors != null)
			{
				echo '<p class="error">' . $errors . '</p>';
			}
			else
			{
				if(mysql_query("UPDATE `scholen` SET `naam`='" . $_POST['name'] . "' WHERE `id`='" . $_GET['school'] . "'"))
				{
					echo '<h1>School</h1><p>De naam van de school is succesvol aangepast.</p>';
					$schoolMade = true;
				}
				else
				{
					echo '<h1>Foutmelding</h1><p>Er is iets mis gegaan tijdens het aanmaken van de school, probeer het alstublieft opnieuw.</p>';
				}
			}
		}

		$nameValue = (isset($_POST['name'])) ? $_POST['name'] : $school['naam'];

		require 'schoolform.php';

		echo 'Indien u deze school wilt verwijderen, klik dan <a href="scholenbeheer/delete_school/' . $school['id'] . '">hier</a>';
	}

	/**
	 * showDeleteSchoolConfirmation()
	 * shows and handles the deletion of a school
	 * 
	 * @param int $schoolID
	 */
	function showDeleteSchoolConfirmation($schoolID)
	{
		$schoolQuery = mysql_query("SELECT * FROM `scholen` WHERE `id`='" . $schoolID . "'");
		
		if(mysql_num_rows($schoolQuery) != 1)
		{
			return "Deze school bestaat niet.";
		}
		
		$school = mysql_fetch_assoc($schoolQuery);
		
		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$this->deleteChilds('school', $schoolID);
			mysql_query("DELETE FROM `scholen` WHERE `id`='" . $schoolID . "'");
			echo '<h1>School verwijderd</h1><p>De school met de naam "' . $school['naam'] . '" is succesvol verwijderd.</p>';
		}
		else
		{
			echo '<h1>Bevestig verwijderen</h1>

			<p>Weet u zeker dat u de school met de naam "' . $school['naam'] . '" wilt verwijderen? Dit kan <strong>niet</strong> ongedaan worden!</p>
			<form method="post"><input type="submit" value="Ja, verwijder deze school" />&nbsp;<input type="button" onclick="location.href=\'scholen.php\'" value="Nee, breng mij terug" /></form>
			';
		}
	}

	/**
	 * showSchool()
	 * prints the classes that the given school contains
	 * 
	 * @param type $schoolID
	 * @return string 
	 */
	function showSchool($schoolID)
	{
		$schoolQuery = mysql_query("SELECT * FROM `scholen` WHERE `id`='" . $schoolID . "'");
		
		if(mysql_num_rows($schoolQuery) != 1)
		{
			return "Deze school bestaat niet.";
		}

		$school = mysql_fetch_assoc($schoolQuery);
		$classesQuery = mysql_query("SELECT * FROM `klassen` WHERE `school_id`='" . $school['id'] . "' ORDER BY `naam` ASC");

		echo '<h1>Klassen behorende tot de school ' . $school['naam'] . '</h1>';
		echo '<a class="button_a" href="scholenbeheer/add_class/' . $school['id'] . '">Klas toevoegen</a>';

		if(mysql_num_rows($classesQuery) > 0)
		{
			echo '<table><tr><th>Naam klas</th><th>Docent</th><th>Acties</th></tr>';

			while($class = mysql_fetch_assoc($classesQuery))
			{
				$docentQuery = mysql_query("SELECT * FROM `docenten` WHERE `klas_id`='" . $class['id'] . "'");

				if(mysql_num_rows($docentQuery) == 1)
				{
					$docent = mysql_fetch_assoc($docentQuery);
					echo '<tr><td>' . $class['naam'] . '</td><td>' . $docent['voornaam'] . ' ' . $docent['achternaam'] . ' (<a href="docentenbeheer/edit/' . $docent['id'] . '">bewerken</a>)</td>';
				}
				else
				{
					echo '<tr><td>' . $class['naam'] . '</td><td>n.v.t. (<a href="docentenbeheer/add/' . $class['id'] . '">toevoegen</a>)</td>';
				}
				echo '<td><a href="scholenbeheer/edit_class/' . $class['id'] . '">bewerken</td></tr>';
			}

			echo '</table>';
		}
		else
		{
			echo '<p>Deze school heeft nog geen klassen.</p>'; 
		}
	}

	/**
	 * showAddClassForm()
	 * shows the form for adding a new class to the given school
	 * 
	 * @param int $schoolID
	 * @return string 
	 */
	function showAddClassForm($schoolID)
	{
		$schoolQuery = mysql_query("SELECT * FROM `scholen` WHERE `id`='" . $schoolID . "'");
		
		if(mysql_num_rows($schoolQuery) != 1)
		{
			return "Deze school bestaat niet.";
		}

		$school = mysql_fetch_assoc($schoolQuery);
		echo '<h1>Klas toevoegen aan de school "' . $school['naam'] . '"</h1>';

		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$errors = $this->verifyAddClassForm($schoolID);

			if($errors != null)
			{
				echo '<p class="error">' . $errors . '</p>';
			}
			else
			{
				if(mysql_query("INSERT INTO `klassen` (naam, school_id) VALUES ('" . $_POST['name'] . "', '" . $schoolID . "')"))
				{
					echo '<h1>Klas toegevoegd</h1><p>De klas met de naam <strong>' . $_POST['name'] . '</strong> is toegevoegd aan de school <strong>' . $school['naam'] . '</strong>. Klik <a href="scholenbeheer/show_school/' . $schoolID . '">hier</a> om terug te gaan, of vul het formulier hieronder opnieuw in om nog een klas toe te voegen.</p>';
					$schoolMade = true;
				}
				else
				{
					echo '<h1>Foutmelding</h1><p>Er is iets mis gegaan tijdens het aanmaken van de school, probeer het alstublieft opnieuw.</p>';
				}
			}
		}
		
		$nameValue = isset($_POST['name']) ? $_POST['name'] : '';

		require 'classform.php';
	}

	/**
	 * verifyAddClassForm
	 * 
	 * @param int $classID
	 * @param int $schoolID
	 */
	private function verifyAddClassForm($classID, $schoolID = null)
	{
		$errors = array();

		// all required forms filled in?
		if(empty($_POST['name']))
		{
			$errors['name'] = 'U moet een naam voor de klas invullen.';
		}

		// username taken?
		if(!isset($errors['name']))
		{
			if(isset($schoolID))
			{
				$usernameTaken = mysql_num_rows(mysql_query("SELECT `id` FROM `klassen` WHERE `naam`='" . $_POST['name'] . "' AND `id` = '" . $classID . "' AND school_id='" . $schoolID . "'"));
			}
			else
			{
				$usernameTaken = mysql_num_rows(mysql_query("SELECT `id` FROM `klassen` WHERE `naam`='" . $_POST['name'] . "' AND `id` = '" . $classID . "'"));
			}

			if($usernameTaken != 0)
			{
				$errors['username'] = 'Er bestaat al een klas met de naam "<strong>' . $_POST['name'] . '</strong>" voor deze school, kies een andere naam.';
			}
		}

		// errors? return them
		if(count($errors) > 0)
		{
			return "<ul><li>" . implode("</li><li>", $errors) . "</li></ul>";
		}
	}

	/**
	 * showEditClassForm()
	 * 
	 * @param int $classID
	 */
	function showEditClassForm($classID)
	{
		$classQuery = mysql_query("SELECT k.id, k.naam, s.naam AS schoolnaam, s.id as schoolid FROM `klassen` AS k, `scholen` AS s WHERE k.id='" . $classID . "' AND k.school_id=s.id");
		if(mysql_num_rows($classQuery) != 1)
		{
			return "Deze school bestaat niet.";
		}
		$class = mysql_fetch_assoc($classQuery);

		echo '<h1>Klas ' . $class['naam'] . ', ' . $class['schoolnaam'] . ' bewerken</h1>';

		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$errors = $this->verifyAddClassForm($classID, $class['schoolid']);

			if($errors != null)
			{
				echo '<p class="error">' . $errors . '</p>';
			}
			else
			{
				if(mysql_query("UPDATE `klassen` SET `naam`='" . $_POST['name'] . "' WHERE `id`='" . $classID . "'"))
				{
					echo '<h1>Klas veranderd</h1><p>De klas met de naam <strong>' . $_POST['name'] . '</strong> veranderd.</p>';
					$schoolMade = true;
				}
				else
				{
					echo '<h1>Foutmelding</h1><p>Er is iets mis gegaan tijdens het aanmaken van de school, probeer het alstublieft opnieuw.</p>';
				}
			}
		}
		
		$nameValue = isset($_POST['name']) ? $_POST['name'] : $class['naam'];

		require 'classform.php';

		echo 'Klik <a href="scholenbeheer/delete_class/' . $classID . '">hier</a> als u deze klas wilt verwijderen.';
	}

	/**
	 * showDeleteClassConfirmation
	 * 
	 * @param int $userID
	 */
	function showDeleteClassConfirmation($userID)
	{
		$ClassQuery = mysql_query("SELECT * FROM `klassen` WHERE `id`='" . $userID . "'");
		
		if(mysql_num_rows($ClassQuery) != 1)
		{
			return "Deze Class bestaat niet.";
		}
		
		$Class = mysql_fetch_assoc($ClassQuery);
		
		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$this->deleteChilds('klas', $userID);
			mysql_query("DELETE FROM `klassen` WHERE `id`='" . $userID . "'");
			echo '<h1>Klas verwijderd</h1><p>De klas met de naam "' . $Class['naam'] . '" is succesvol verwijderd.</p>';
		}
		else
		{
			echo '<h1>Bevestig verwijderen</h1>

			<p>Weet u zeker dat u de Klas met de naam "' . $Class['naam'] . '" wilt verwijderen? Ook alle leerlingen die tot deze klas behoren zullen worden verwijderd. Dit kan <strong>niet</strong> ongedaan worden!</p>
			<form method="post"><input type="submit" value="Ja, verwijder deze Klas" />&nbsp;<input type="button" onclick="location.href=\'scholen.php\'" value="Nee, breng mij terug" /></form>
			';
		}
	}

	/**
	 * deleteChilds
	 * Deletes the childs of the given type of data (School->Class->Students)
	 * 
	 * @param string $type The type of the parent
	 * @param int $id The ID of the parent
	 */
	private function deleteChilds($type, $id)
	{
		if($type == 'school')
		{
			mysql_query("DELETE FROM `leerlingen` WHERE `klas_id` IN (SELECT `id` FROM `klassen` WHERE `school_id`=" . $id . ")");
			mysql_query("DELETE FROM `docenten` WHERE `klas_id` IN (SELECT `id` FROM `klassen` WHERE `school_id`=" . $id . ")");
			mysql_query("DELETE FROM `klassen` WHERE `klas_id`=" . $id);
		}
		else
		{
			mysql_query("DELETE FROM `docenten` WHERE `klas_id`='" . $id . "'");
			mysql_query("DELETE FROM `leerlingen` WHERE `klas_id`='" . $id . "'");
		}
	}
} 